# coding=utf-8
# __author__ = "James Zhang"
# !/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import six
from importlib import import_module


def import_string(dotted_path):
    """通过字符串的形式,导入模块,
    例如:
    # import_string("core.sessions.serializers.JSONSerializer")
    等效于: from core.sessions.serializers import JSONSerializer.

    :param dotted_path:
    :return:
    """
    try:
        module_path, class_name = dotted_path.rsplit('.', 1)
    except ValueError:
        msg = "%s doest't look like a module path" % dotted_path
        six.reraise(ImportError, ImportError(msg), sys.exc_info()[2])
    module = import_module(module_path)
    try:
        return getattr(module, class_name)
    except AttributeError:
        msg = 'Module "%s" does not define a "%s" attribute/class' % (
            module_path, class_name)
        six.reraise(ImportError, ImportError(msg), sys.exc_info()[2])
